package io.renren.modules.clothingpicture.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.JacksonUtils;
import io.renren.modules.clothingpicture.dao.ClothingPictureDao;
import io.renren.modules.clothingpicture.dto.ClothingPictureDTO;
import io.renren.modules.clothingpicture.entity.ClothingPictureEntity;
import io.renren.modules.clothingpicture.service.ClothingPictureService;
import io.renren.modules.clothingpicture.vo.ClothingPictureVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 衣服图片表
 *
 * @author lzy lzy
 * @since 1.0.0 2021-08-30
 */
@Slf4j
@Service
public class ClothingPictureServiceImpl extends ServiceImpl<ClothingPictureDao, ClothingPictureEntity> implements ClothingPictureService {


    @Override
    public void save(ClothingPictureDTO clothingPictureDTO) {
        ClothingPictureEntity clothingPictureEntity = JacksonUtils.obj2pojo(clothingPictureDTO, ClothingPictureEntity.class);
        this.save(clothingPictureEntity);
    }

    @Override
    public void update(ClothingPictureDTO clothingPictureDTO) {
        ClothingPictureEntity clothingPictureEntity = JacksonUtils.obj2pojo(clothingPictureDTO, ClothingPictureEntity.class);
        this.updateById(clothingPictureEntity);
    }

    @Override
    public ClothingPictureVO queryPictureByTypeId(Long typeId) {
        log.info("根据服装类型表id查询图片");
        QueryWrapper<ClothingPictureEntity> wrapper = new QueryWrapper<ClothingPictureEntity>();
        wrapper.eq("clothing_type_id",typeId);
        ClothingPictureEntity clothingPictureEntity = this.baseMapper.selectOne(wrapper);
        return JacksonUtils.obj2pojo(clothingPictureEntity,ClothingPictureVO.class);
    }

    @Override
    public void updatePicture(Long typeId,String pictureUrl) {
        log.info("根据服装类型表id更新图片");
        QueryWrapper<ClothingPictureEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("clothing_type_id",typeId);
        this.baseMapper.delete(wrapper);
        ClothingPictureEntity entity = new ClothingPictureEntity();
        entity.setClothingTypeId(typeId);
        entity.setPicture(pictureUrl);
        this.baseMapper.insert(entity);
    }

    @Override
    public void deleteTypeId(Long id) {
        log.info("根据服装类型表id删除图片");
        QueryWrapper<ClothingPictureEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("clothing_type_id",id);
        this.baseMapper.delete(wrapper);
    }


}
